/*******************************************************************
 * 
 *  Copyright C 2005 by Amlogic, Inc. All Rights Reserved.
 *
 *  Description: 
 *
 *  Author: Amlogic Software
 *  Created: Fri Nov 11 01:17:34 2005
 *
 *******************************************************************/
#include "../menu.h"
#include "user.h"
#include "../aw_TXT_app.h"
#include "ctrlconvert.h"
#include "../resource/string_dat.h"
#include "LogoEvent.h"

#define DEBUG_KEY_NUM  4
INT8U debug_display_flag = 0;
INT8U debug_key_in[DEBUG_KEY_NUM] = 
{
  UOP_9, UOP_8, UOP_7, UOP_6
};
INT8U debug_key_out[DEBUG_KEY_NUM] = 
{
  UOP_6, UOP_7, UOP_8, UOP_9
};
PHWND Debug_Prev_Win = NULL;

#define AW_DEEP_BLUE_COLOR       3
static INT32U txt_page = 0xffffffff;
static INT32U txt_goto_flag = 0;
static char txt_page_str[10] = "";
static PIXELVAL old_color;

void AWDetectDebug(INT32U *iInputKey){}

void AWDebugClr()
{
  debug_string[0][0] = 0;
  debug_string[1][0] = 0;
  debug_string[2][0] = 0;
  debug_string[3][0] = 0;
  debug_string[4][0] = 0;
  debug_string[5][0] = 0;
  debug_string[6][0] = 0;
  debug_string[7][0] = 0;
  debug_string[8][0] = 0;
  debug_string[9][0] = 0;
  debug_string[10][0] = 0;
  debug_string[11][0] = 0;
  debug_string[12][0] = 0;
  debug_string[13][0] = 0;
  debug_string[14][0] = 0;
  debug_string[15][0] = 0;
  debug_string[16][0] = 0;
  debug_string[17][0] = 0;
  debug_string[18][0] = 0;
  debug_string[19][0] = 0;




}

INT32S AWDebugOnShow(PHWND hwnd, INT32S lpParam)
{
  old_color = AWSetForeground(AW_DEEP_BLUE_COLOR);
  AWDebugClr();
  debug_page[0] = 0;
  txt_page = 0xffffffff;
  txt_goto_flag = 0;
  txt_page_str[0] = 0;
  return 0;
}

INT32S AWDebugOnHide(PHWND hwnd, INT32S lpParam)
{
  AWSetForeground(old_color);
  return 0;
}

INT32S AWDebugOnPaint(PHWND hwnd, INT32S lpParam)
{
  PIXELVAL old_color;

  hwnd->pClass->ClassProc(hwnd, WM_PAINT, lpParam); //call original fun first
  return 1;
}

static void DebugShowPage(PHWND hwnd)
{
  sprintf(debug_page, "%s_/%d", txt_page_str, (txt_page &0xffff));
  AWSendMessage(hwnd, WM_PAINT_DRAW_INFO_CENTER, 0);
}

static void DebugProcessNum(PHWND hwnd, INT32U num)
{
  if ((num == 0) && (txt_goto_flag == 1))
  {
    return ;
  }
  txt_page_str[txt_goto_flag++ - 1] = num + '0';
  txt_page_str[txt_goto_flag - 1] = 0;
  if (atoi(txt_page_str) > (txt_page &0xffff))
  {
    txt_goto_flag--;
    txt_page_str[txt_goto_flag - 1] = 0;
  }
  DebugShowPage(hwnd);
}

INT32S DebugPageOnKeyPress(PHWND hwnd, INT32S lpParam)
{

  INT32U *key_value = (INT32U*)lpParam;

  switch (*key_value)
  {
    case UOP_RESUME:
    case UOP_STOP:
        extern char txt_read_finish;
        if(txt_read_finish == TRUE)
        {
        #if (defined TXT_PLAYER)&&(!defined AML_DIR)
            extern INT8U WinTypeChoice;
            if(WinTypeChoice == TXT)
                GUIShowWindow(WinLogo);
            else
        #endif
                GUIShowWindow(WinFileList);
        }
      break;
    case UOP_ENTER:
      if (txt_goto_flag && (txt_page_str[0] != 0))
      {
        AWTXTKeyDo(UOP_TIME_PLAY, (atoi(txt_page_str) - 1));
      }
      break;
    case UOP_TIME_PLAY:
      if ((!txt_goto_flag) && (txt_page != 0xffffffff))
      {
        txt_page_str[0] = 0;
        txt_goto_flag = 1;
        DebugShowPage(hwnd);
      }
      break;
    case UOP_0:
    case UOP_1:
    case UOP_2:
    case UOP_3:
    case UOP_4:
    case UOP_5:
    case UOP_6:
    case UOP_7:
    case UOP_8:
    case UOP_9:
      DebugProcessNum(hwnd, (*key_value - UOP_0));
      break;
  }
  return 0;
}


INT32S DebugPageOnPaint(PHWND hwnd, INT32S lpParam)
{
  AWSendMessage(hwnd, WM_PAINT_DRAW_INFO_CENTER, 0);
  return 1;
}

INT32S UpdateDebugPage(INT32U page, INT8U flag)
{
  PHWND hwnd = AWFindWinCtrlByName(DebugPage);

  if ((page == txt_page) && (txt_goto_flag == 0))
  {
    return 0;
  }
  txt_goto_flag = 0;
  txt_page_str[0] = 0;
  txt_page = page;
  if (flag)
  {
    sprintf(debug_page, "%d/%d", ((page &0xffff0000) >> 16), (page &0xffff));
  }
  else
  {
    sprintf(debug_page, "%d%%", page);
  }
  if (hwnd == NULL)
    return 0;
  if (AWIsVisibleWinControl(hwnd))
  {
    AWClearRectTxt(hwnd->draw_info[0].draw_pos_x, hwnd->draw_info[0].draw_pos_y,
      hwnd->draw_info[0].draw_width, hwnd->draw_info[0].draw_height);
    AWPostMessage(hwnd, WM_PAINT_DRAW_INFO_CENTER, 0);
  }
  return 0;
}
